package com.xtaifghy.wugui;

import com.xtaifghy.wugui.util.Util;

import java.util.ArrayList;
import java.util.Random;

public class GameState3 implements GameStateInterface{
    private int state = 0b11111111;
    private int goalState = 0b00000010;
    Random rand = new Random();
    private static int[] actions={
            0b1011,//{0,1,3},按钮0
            0b111,//{0,1,2},按钮1
            0b10110,//{1,2,4},按钮2
            0b101001,//{0,5,3},按钮3
            0b1011010,//{1,4,6,3},按钮4
            0b10010100,//{2,4,7},按钮5
            0b1101000,//{6,5,3},按钮6
            0b11100000,//{6,5,7},按钮7
            0b11010000,//{6,4,7},按钮8
    };

    public void doAction(int a){
        state = state^actions[a];
    }

    public boolean testState(){
        return(state==goalState);
    }

    public void setState(boolean[] state) {
        this.state = Util.bArrToInt(state);
    }

    public void setGoalState(boolean[] goalState) {
        this.goalState = Util.bArrToInt(goalState);
    }

    public ArrayList<boolean[]> getPaths(){
        ArrayList<boolean[]> paths = new ArrayList<>();
        GameState3 gs = new GameState3();
        gs.setState(getState());
        gs.setGoalState(getGoalState());
        treeSearch(gs,new boolean[9],0,paths);
        return paths;
    }

    private void treeSearch(GameStateInterface gs, boolean[] action, int depth, ArrayList<boolean[]>paths){
        //if(paths.size()>0) return;
        if(depth==9){
            if(gs.testState()){
                paths.add(action.clone());
            }
        }else if(depth<9){
            action[depth]=true;
            gs.doAction(depth);
            treeSearch(gs,action,depth+1,paths);
            gs.doAction(depth);
            action[depth]=false;
            treeSearch(gs,action,depth+1,paths);
        }
    }

    public GameState3() {
        //randomGoalState();
    }

    public boolean[] getState() {
        return Util.intToBArr(state,8);
    }

    public boolean[] getGoalState() {
        return Util.intToBArr(goalState,8);
    }

    public void randomGoalState(){
            goalState = rand.nextInt(256);
    }
}
